#include <bits/stdc++.h>
#define lli long long int
#define ii pair<lli,lli>
#define fi first
#define se second
using namespace std;
int main ()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t; cin>>t;
while (t--)
{
int n,k; cin>>n>>k;
lli a[n+1];
for (int i=1;i<=n;i++) cin>>a[i];
if (k!=4)
{
lli mn=1e18;
for (int i=1;i<=n;i++)
if (a[i]%k==0) mn=0; else mn=min(mn,k-a[i]%k);
cout<<mn<<"\n";
}
else
{
bool flag=false; lli kq=1e18;
for (int i=1;i<=n;i++)
if (a[i]%4==0) kq=0;
if (kq)
{
int dem=0;
for (int i=1;i<=n;i++)
if (a[i]%2==0) dem++;
if (dem>=2) cout<<"0\n";
else if (dem==1 && n==1) cout<<"2\n";
else if (dem==1) cout<<"1\n";
else
{
bool ok=false;
for (int i=1;i<=n;i++)
if (a[i]%4==3) ok=true;
if (ok) cout<<"1\n";
else if (!ok && n==1) cout<<"3\n";
else cout<<"2\n";
}
}
else cout<<"0\n";
}
}
}
1475A - Odd Divisor | 1454B - Unique Bid Auction |
978C - Letters | 501B - Misha and Changing Handles |
1496A - Split it | 1666L - Labyrinth |
1294B - Collecting Packages | 1642B - Power Walking |
1424M - Ancient Language | 600C - Make Palindrome |
1669D - Colorful Stamp | 1669B - Triple |
1669A - Division | 1669H - Maximal AND |
1669E - 2-Letter Strings | 483A - Counterexample |
3C - Tic-tac-toe | 1669F - Eating Candies |
1323B - Count Subrectangles | 991C - Candies |
1463A - Dungeon | 1671D - Insert a Progression |
1671A - String Building | 1671B - Consecutive Points Segment |
1671C - Dolce Vita | 1669G - Fall Down |
4D - Mysterious Present | 1316B - String Modification |
1204A - BowWow and the Timetable | 508B - Anton and currency you all know |